<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Settings_model extends CI_Model {

	public function __construct() {
		parent::__construct();
		
	}
	
	
	public function getSettings(){
		$sql = "select * from web_settings";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $result;
		}else{
			return null;
		}
		
	}
	
	function getSetting($name) {
		if (empty($name)) {
			return $name;
		} else {
			$sql = "Select value from web_settings where variable = '$name' limit 0, 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0) {
				$row = $query->row();
				return $row->value;
			}
		}
	}
	
	function siteInfo($name){
		if (empty($name)) {
			return $name;
		} else {
			$sql = "Select $name from web_configuration limit 0, 1";
			
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0) {
				$row = $query->row();
				return $row->$name;
			}
		}
	}
	
	
	function get_setting($name) {
		if (empty($name)) {
			return $name;
		} else {
			$sql = "Select value from web_settings where variable = '$name'";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0) {
				$row = $query->row();
				return $row->value;
			}
		}
	}
	
	function date_format($format='Africa/Lagos',$strDate){
		$dt=date($format, strtotime($strDate));
		return $dt;
	}
	
	function get_web_configuration($fieldName) {
		$sql = "SELECT $fieldName FROM web_configuration";
		$query = $this->db->query($sql);
		$result = $query->result();
		$row = $query->row();
		return $row->$fieldName;
	}
	
	function send_mail($email = NULL, $subject = NULL, $body = NULL) {
	
		$this->email->clear();
		//--Email Config
		$email_config['protocol'] = 'mail';
		$email_config['wordwrap'] = FALSE;
		$email_config['mailtype'] = 'html';
		$email_config['charset'] = 'utf-8';
		$email_config['crlf'] = "\r\n";
		$email_config['newline'] = "\r\n";
		$this->email->initialize($email_config);
	
		$this->email->to($email);
		$this->email->from($this->settings_model->get_web_configuration('default_email'), $this->settings_model->get_web_configuration('web_title'));
		$this->email->subject($subject);
		$this->email->message($body);
		$this->email->send();
	}
	function getTips($limit=5){
		$sql = "select * from tips where status = 51 order by RAND() limit $limit";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $result;
		}else{
			return null;
		}
	}
	
	function get_socials($id) {
	
		$sql = "select * from social where id=$id";
		$query = $this->db->query($sql);
		$result = $query->row();
		return $result;
	}
	
	function upload($imageData=NULL){
	
		if ($imageData['imagearray']['error'] == 0) {
	
			$ext = preg_replace("/.*\.([^.]+)$/", "\\1", strtolower($imageData['imagearray']['name']));
			$imagename=strtolower($imageData['imagearray']['name']);
			$imagename=md5(time());
			//die($imagename);
				
			$imagename=explode( '.',$imagename);
			$imagename=$imagename[0];
	
			if($imageData['customefilename']){
				$filename=$imageData['customefilename'].'.'.$ext;
			}
			else{
				$filename = $imagename.'.'.$ext;
			}
	
			if($imageData['namewithtime']=='Yes'||$imageData['namewithtime']=='yes'){
				$filename=$imagename.time().'.'.$ext;
				if($imageData['separator'])
					$filename=$imagename.$imageData['separator'].'.'.$ext;
			}
			if($imageData['prefix'])
				$filename=$imageData['prefix'].$filename;
	
			$config['file_name'] = strtolower($filename);
			$config['upload_path'] = $imageData['imagepath'];
			$config['allowed_types'] = $imageData['allowedtype'];
			$config['overwrite'] = $imageData['overwrite'];
			$config['remove_spaces'] = $imageData['removespace'];
	
	
			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('userfile')) {
	
				if($imageData['errmessage'])
					$imagedata=array('message'=>$imageData['errmessage'],'data'=>"" ,'code'=>0);
				else
					$imagedata=array('message'=>$this->upload->display_errors(),'data'=>"",'code'=>0);
				return $imagedata;
			}
			//if sucessfull
	
	
	
			else{
				if($imageData['createthumb']=='Yes'||$imageData['createthumb']=='yes')
				{
						
					$ext = preg_replace("/.*\.([^.]+)$/", "\\1", $filename);
	
					$img=explode( '.',$filename);
					$imgfile=$img[0];
						
					$imgfilename = $imgfile.'_thumb'.'.'.$ext;
						
					//print $filename;
					//die();
						
						
					//$thumb['imgFile'] =
					$thumb['image_library'] = 'gd2';
					$thumb['source_image'] = $imageData['imagepath'].$filename;
					$thumb['new_image'] = $imageData['thumbpath'];
					$thumb['create_thumb'] = true;
					$thumb['maintain_ratio'] = false;
					$thumb['thumb_marker']  = '_thumb' ;
					$thumb['width'] = $imageData['thumbwidth'];
					$thumb['height'] = $imageData['thumbheight'];
	
					$this->load->library('image_lib', $thumb);
					$this->image_lib->resize();
					if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
					$this->image_lib->clear();
				}
				if($imageData['sucessmessage'])
					$imagedata=array('message'=>$imageData['sucessmessage'],'data'=>$imgfilename,'code'=>1);
				else
					$imagedata=array('message'=>"Image Uploded Sucesfully...",'data'=>$imgfilename,'code'=>1);
				return $imagedata;
			}
		}
	}
	
}	
/* End of file*/	
